package image;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

public class CropImage{
	public CropImage(){}
	public byte[] cropImage(byte[] imagebytes, String leftX, String topY, String rightX, String bottomY){
		ImagesService imagesService = ImagesServiceFactory.getImagesService();
        Image oldImage = ImagesServiceFactory.makeImage(imagebytes);
        //Do crop image
        double leftX_double = Double.parseDouble(leftX);
        double topY_double = Double.parseDouble(topY);
        double rightX_double = Double.parseDouble(rightX);
        double bottomY_double = Double.parseDouble(bottomY);
        //double width_double = Double.parseDouble(width);
        //double height_double = Double.parseDouble(height);
        Transform crop = ImagesServiceFactory.makeCrop(leftX_double, topY_double, rightX_double, bottomY_double);
        //Create a new image from the result
        Image newImage = imagesService.applyTransform(crop, oldImage);
        byte[] newImageData = newImage.getImageData();
        return newImageData;
	}
}